package com.company.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.company.model.CompanyIntangible;
import com.company.service.IntangibleService;

@Controller
public class IntangibleController {

	public static final String toIntangibleView = "/company/intangible";
	public static final String intangibleList = "/company/intangible/list";
	public static final String openIntangibleEditView = "/company/intangible/editView";
	public static final String intangibleDelete = "/company/intangible/delete";
	public static final String openIntangibleAddView = "/company/intangible/addView";
	public static final String intangibleSave = "/company/intangible/save";
	// 页面路径
	public static final String intangiblePage = "/company/intangible";
	public static final String intangibleListPage = "/company/intangibleList";
	public static final String intangibleEditPage = "/company/intangibleEdit";
	public static final String intangibleAddPage = "/company/intangibleAdd";

	@Autowired
	IntangibleService intangibleService;

	@RequestMapping(value = toIntangibleView, method = RequestMethod.POST)
	public ModelAndView toIntangibleView(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mv = new ModelAndView(intangiblePage);
		String companyId = req.getSession().getAttribute("companyId").toString();
		List<CompanyIntangible> list = null;
		try {
			list = intangibleService.findIntangibleByCompanyId(companyId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("intangibleList", list);
		return mv;
	}
	
	@RequestMapping(value = intangibleList, method = RequestMethod.POST)
	public ModelAndView intangibleList(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mv = new ModelAndView(intangibleListPage);
		String companyId = req.getSession().getAttribute("companyId").toString();
		List<CompanyIntangible> list = null;
		try {
			list = intangibleService.findIntangibleByCompanyId(companyId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("intangibleList", list);
		return mv;
	}

	@RequestMapping(value = openIntangibleEditView, method = RequestMethod.POST)
	public ModelAndView openIntangibleEditView(HttpServletRequest req, HttpServletResponse res, String id) {
		ModelAndView mv = new ModelAndView(intangibleEditPage);
		CompanyIntangible companyIntangible = null;
		try {
			companyIntangible = intangibleService.findIntangibleById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("intangible", companyIntangible);
		return mv;
	}

	@RequestMapping(value = openIntangibleAddView, method = RequestMethod.POST)
	public ModelAndView openIntangibleAddView(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mv = new ModelAndView(intangibleAddPage);
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = intangibleDelete, method = RequestMethod.POST)
	public boolean intangibleDelete(String ids) {
		boolean result = false;
		try {
			result = intangibleService.deleteIntangibleByIds(ids);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = intangibleSave, method = RequestMethod.POST)
	public boolean intangibleSave(HttpServletRequest req, HttpServletResponse res, CompanyIntangible companyIntangible) {
		String companyId = req.getSession().getAttribute("companyId").toString();
		boolean result = false;
		try {
			companyIntangible.setCompanyId(companyId);
			companyIntangible.setCreateTime(new Date());
			result = intangibleService.saveIntangible(companyIntangible);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
